package OneList;

public class MyList implements IList{
    static class Node{
        Node prv;
        Node next;
        int x;
        public Node(int x) {
            this.x = x;
        }
    }

    private int size=0;
    private Node head=null;
    private Node tail=null;

    @Override
    public void addFirst(int data) {
        Node node=new Node(data);
if (head==null){
    head=node;
    tail=node;
}else {
    node.next=head;
    head.prv=node;
    head=node;
}
size++;
    }

    @Override
    public void addLast(int data) {
        Node node=new Node(data);
        if (head==null){
            head=node;
            tail=node;
        }else {
        node.prv=tail;
        tail.next=node;
        tail=node;
        }
        size++;
    }

    @Override
    public void addIndex(int index, int data) {
        Node node=new Node(data);
if (index<0||index>size){
    return ;
}
if (index==0){
    addFirst(data);
}
else if (index==size){
    addLast(data);
}else {
    Node cur=head;
    while (index>1){
        cur=cur.next;
        index--;
    }
    Node t=cur.next;
    node.next=t;
    t.prv=node;
    cur.next=node;
    node.prv=cur;
    size++;
}
    }

    @Override
    public boolean contains(int key) {
        if (head==null){
            return false;
        }
        Node cur=head;
        while (cur!=null){
            if(cur.x==key){
                return true;
            }
            cur=cur.next;
        }
        return  false;
    }

    @Override
    public void remove(int key) {
        if (head==null){
            return;
        }

        if (head.x==key){
            head=head.next;
            head.prv=null;
            size--;
        }
        Node cur=head;
        while (cur.next!=null){
            if(cur.x==key){
                cur.prv.next=cur.next;
                cur.next.prv=cur.prv;
                size--;
                return;
            }
            cur=cur.next;
        }
        if (cur.x==key){
            tail=tail.prv;
            tail.next=null;
            size--;
        }
    }

    @Override
    public void removeAllKey(int key) {
if (head==null){
    return;
}
Node cur=head;
while (cur!=null){
    if(cur.x==key&&cur==head){
        head=head.next;
        cur=cur.next;
        head.prv=null;
        size--;
    } else if (cur.x==key&&cur==tail) {
        tail=tail.prv;
        tail.next=null;
        size--;
    }else if (cur.x==key){
        cur.prv.next=cur.next;
        cur.next.prv=cur.prv;
        cur=cur.next;
        size--;
    }else {
        cur=cur.next;
    }
}


    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {
Node cur=head;
Node next=cur.next;
while (next!=null){
    cur=null;
    cur=next;
    next=next.next;
}
cur=null;
head=null;
tail=null;
    }

    @Override
    public void display() {
if (head==null){
    return;
}
Node cur=head;
while (cur!=null){
    System.out.print(cur.x+" ");
    cur=cur.next;
}
        System.out.println();
    }
}
